<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>复习单词 - 单词记忆</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background: linear-gradient(135deg, #e8f4f8 0%, #f0f8fb 100%);
            background-image:
                radial-gradient(circle at 20% 30%, rgba(64, 158, 255, 0.08) 0%, transparent 25%),
                radial-gradient(circle at 80% 70%, rgba(64, 158, 255, 0.08) 0%, transparent 25%);
            background-attachment: fixed;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        .container {
            background-color: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 16px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
            padding: 40px 30px;
            max-width: 700px;
            width: 100%;
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.8);
            min-height: 550px;
        }
        .icon {
            font-size: 80px;
            margin-bottom: 30px;
            display: block;
        }
        h1 {
            color: #2c3e50;
            font-size: 28px;
            margin-bottom: 10px;
        }
        .description {
            color: #606266;
            font-size: 14px;
            margin-bottom: 20px;
        }
        .back-btn {
            padding: 12px 30px;
            background: linear-gradient(90deg, #409eff, #67c2f9);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
        }
        .back-btn:hover {
            background: linear-gradient(90deg, #3088e0, #409eff);
            box-shadow: 0 6px 16px rgba(64, 158, 255, 0.3);
            transform: translateY(-2px);
        }
        /* 复习区域 */
        .review-area {
            width: 100%;
            margin: 30px 0;
        }
        .word-info {
            background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #409eff;
        }
        .phonetic {
            font-size: 18px;
            color: #606266;
            margin: 10px 0;
        }
        .translation {
            font-size: 16px;
            color: #2c3e50;
            margin: 10px 0;
            font-weight: 500;
        }
        .input-area {
            margin: 20px 0;
        }
        .word-input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e0e6ed;
            border-radius: 8px;
            font-size: 18px;
            text-align: center;
            transition: all 0.3s ease;
        }
        .word-input:focus {
            outline: none;
            border-color: #409eff;
            box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.1);
        }
        .word-input.correct {
            border-color: #67c23a;
            background-color: #f0f9ff;
        }
        .word-input.wrong {
            border-color: #f56c6c;
            background-color: #fef0f0;
        }
        .button-group {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 20px;
        }
        .btn {
            padding: 12px 30px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
        }
        .btn-primary {
            background: linear-gradient(90deg, #409eff, #67c2f9);
            color: white;
            box-shadow: 0 4px 12px rgba(64, 158, 255, 0.2);
        }
        .btn-primary:hover:not(:disabled) {
            background: linear-gradient(90deg, #3088e0, #409eff);
            box-shadow: 0 6px 16px rgba(64, 158, 255, 0.3);
            transform: translateY(-2px);
        }
        .btn-secondary {
            background: linear-gradient(90deg, #e6a23c, #f0c78a);
            color: white;
            box-shadow: 0 4px 12px rgba(230, 162, 60, 0.2);
        }
        .btn-secondary:hover {
            background: linear-gradient(90deg, #d4941d, #e6a23c);
            transform: translateY(-2px);
        }
        .btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            box-shadow: none;
        }
        .feedback {
            text-align: center;
            margin-top: 15px;
            padding: 12px;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 500;
            display: none;
        }
        .feedback.show {
            display: block;
        }
        .feedback.correct {
            background-color: #f0f9ff;
            color: #67c23a;
            border: 1px solid #c6f6d5;
        }
        .feedback.wrong {
            background-color: #fef0f0;
            color: #f56c6c;
            border: 1px solid #fde2e2;
        }
        .progress {
            text-align: center;
            color: #909399;
            font-size: 14px;
            margin-bottom: 20px;
        }
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #909399;
        }
        .empty-state::before {
            content: '🎓';
            display: block;
            font-size: 60px;
            margin-bottom: 20px;
            opacity: 0.5;
        }
        .loading {
            text-align: center;
            padding: 40px;
            color: #909399;
        }
        .loading-spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(64, 158, 255, 0.2);
            border-top-color: #409eff;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            margin-right: 10px;
        }
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        .complete-message {
            text-align: center;
            padding: 40px 20px;
        }
        .complete-message::before {
            content: '🎉';
            display: block;
            font-size: 60px;
            margin-bottom: 20px;
        }
        .complete-message h2 {
            color: #67c23a;
            margin-bottom: 10px;
        }
    </style>
    <script>
        let reviewWords = [];
        let currentIndex = 0;
        let hintUsed = false;
        
        async function checkLogin() {
            try {
                const response = await fetch('/api/user', { credentials: 'include' });
                if (!response.ok) {
                    window.location.href = '/login.html';
                    return false;
                }
                return true;
            } catch (err) {
                window.location.href = '/login.html';
                return false;
            }
        }
        
        // 加载复习单词
        async function loadReviewWords() {
            const reviewArea = document.getElementById('reviewArea');
            reviewArea.innerHTML = '<div class="loading"><span class="loading-spinner"></span>加载中...</div>';
            
            try {
                const response = await fetch('/api/review-words', {
                    credentials: 'include'
                });
                
                if (!response.ok) {
                    throw new Error('加载失败');
                }
                
                reviewWords = await response.json();
                
                if (reviewWords.length === 0) {
                    reviewArea.innerHTML = '<div class="empty-state">暂无已学单词，去学习一些单词吧！</div>';
                    return;
                }
                
                currentIndex = 0;
                renderCurrentWord();
                
            } catch (err) {
                console.error('加载复习单词失败：', err);
                reviewArea.innerHTML = '<div class="empty-state">加载失败，请重试</div>';
            }
        }
        
        // 渲染当前单词
        function renderCurrentWord() {
            if (currentIndex >= reviewWords.length) {
                showComplete();
                return;
            }
            
            const word = reviewWords[currentIndex];
            hintUsed = false;
            
            const reviewArea = document.getElementById('reviewArea');
            reviewArea.innerHTML = `
                <div class="progress">进度: ${currentIndex + 1} / ${reviewWords.length}</div>
                <div class="word-info">
                    <div class="phonetic">
                        <strong>美式音标：</strong>${word.usphone || '无'}<br>
                        <strong>英式音标：</strong>${word.ukphone || '无'}
                    </div>
                    <div class="translation">
                        <strong>翻译：</strong>${word.transCn}
                    </div>
                </div>
                <div class="input-area">
                    <input type="text" class="word-input" id="wordInput" 
                           placeholder="请输入单词" autocomplete="off">
                </div>
                <div class="button-group">
                    <button class="btn btn-secondary" onclick="showHint()">提示</button>
                    <button class="btn btn-primary" onclick="checkAnswer()">提交</button>
                </div>
                <div class="feedback" id="feedback"></div>
            `;
            
            // 自动聚焦输入框
            setTimeout(() => {
                document.getElementById('wordInput').focus();
            }, 100);
            
            // 支持回车键提交
            document.getElementById('wordInput').addEventListener('keypress', (e) => {
                if (e.key === 'Enter') {
                    checkAnswer();
                }
            });
        }
        
        // 显示提示
        function showHint() {
            if (hintUsed) return;
            
            const word = reviewWords[currentIndex];
            const wordInput = document.getElementById('wordInput');
            const feedback = document.getElementById('feedback');
            
            // 显示前三个字母
            const hint = word.headWord.substring(0, 3);
            wordInput.value = hint;
            hintUsed = true;
            
            feedback.className = 'feedback show';
            feedback.style.backgroundColor = '#fff7e6';
            feedback.style.color = '#e6a23c';
            feedback.style.border = '1px solid #f0c78a';
            feedback.textContent = `提示：单词以 "${hint}..." 开头`;
            
            wordInput.focus();
        }
        
        // 检查答案
        function checkAnswer() {
            const word = reviewWords[currentIndex];
            const wordInput = document.getElementById('wordInput');
            const userAnswer = wordInput.value.trim().toLowerCase();
            const correctAnswer = word.headWord.toLowerCase();
            const feedback = document.getElementById('feedback');
            
            if (!userAnswer) {
                feedback.className = 'feedback show wrong';
                feedback.textContent = '请输入单词！';
                return;
            }
            
            if (userAnswer === correctAnswer) {
                wordInput.className = 'word-input correct';
                feedback.className = 'feedback show correct';
                feedback.textContent = '✅ 回答正确！';
                
                // 1.5秒后自动下一个
                setTimeout(() => {
                    currentIndex++;
                    renderCurrentWord();
                }, 1500);
            } else {
                wordInput.className = 'word-input wrong';
                feedback.className = 'feedback show wrong';
                feedback.textContent = `❌ 回答错误！正确答案是：${word.headWord}`;
                
                // 2秒后自动下一个
                setTimeout(() => {
                    currentIndex++;
                    renderCurrentWord();
                }, 2000);
            }
        }
        
        // 显示完成页面
        function showComplete() {
            const reviewArea = document.getElementById('reviewArea');
            reviewArea.innerHTML = `
                <div class="complete-message">
                    <h2>恭喜你！</h2>
                    <p>你已经完成所有单词的复习！</p>
                    <div class="button-group" style="margin-top: 30px;">
                        <button class="btn btn-primary" onclick="loadReviewWords()">重新开始</button>
                        <button class="btn btn-secondary" onclick="window.location.href='/home.html'">返回首页</button>
                    </div>
                </div>
            `;
        }
        
        window.onload = async () => {
            const isLoggedIn = await checkLogin();
            if (isLoggedIn) {
                loadReviewWords();
            }
        };
    </script>
</head>
<body>
<div class="container">
    <span class="icon">🔄</span>
    <h1>单词复习</h1>
    <p class="description">根据音标和翻译默写单词</p>
    
    <div class="review-area" id="reviewArea">
        <div class="loading"><span class="loading-spinner"></span>加载中...</div>
    </div>
    
    <button class="back-btn" onclick="window.location.href='/home.html'">返回首页</button>
</div>
</body>
</html>
